#include "cwinengine.h"

namespace NEngine {

// ================================================================================================ 
// Static method for windproc
// ================================================================================================
LRESULT CALLBACK CWindow::WndProc(HWND lhwnd, UINT msg, WPARAM wpar, LPARAM lpar) {
    PAINTSTRUCT ps;
    HDC hdc;

    switch(msg) {
        case WM_PAINT:
    	    hdc = BeginPaint(lhwnd, &ps );
    	    EndPaint(lhwnd, &ps);
    	    break;

    	case WM_DESTROY:
    	    PostQuitMessage(0);
    	    break;

    	default:
    	    return DefWindowProc(lhwnd, msg, wpar, lpar);
    }

    return 0;
}

// ================================================================================================
// Initialize the window
// ================================================================================================
HRESULT CWindow::InitWindow(unsigned int width, unsigned int height) {
    // Register class
    WNDCLASSEX wcex;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = CWindow::WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hinst;
    wcex.hIcon = LoadIcon(hinst, IDI_APPLICATION);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = L"TutorialWindowClass";
    wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
    if(!RegisterClassEx(&wcex))
        return E_FAIL;

    // Create window
    rc.left = 0;
    rc.top = 0;
    rc.right = width;
    rc.bottom = height;
    AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
    hwnd = CreateWindow(L"TutorialWindowClass", L"Crappy Graphics", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hinst,
        NULL);
    if(!hwnd)
        return E_FAIL;

    ShowWindow(hwnd, cmdshow);

    return S_OK;
}

} // namespace NEngine